/*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */

    package puzzleevolution;

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;

    /**
     *
     * @author vorispe1
     */
    public class PuzzleInit extends Component {

        private int bf;


        /**
         * Metoda nacte dilek na udane pozici v obrazku.
         *
         * @param x     horizontalni poradi dilku
         * @param y     vertikalni poradi dilku
         * @return      dilek s vyplnenou pixelovou plochou
         * @throws IOException
         */
        public PuzzlePiece readPiece(Picture picture, int x, int y) throws IOException{
            PuzzlePiece piece = new PuzzlePiece();
            int offsetX, offsetY;
            //posunuti pocatku podle zadanych souradnic dilku
            offsetX = x * Config.delkaDilku;
            offsetY = y * Config.delkaDilku;

            BufferedImage img;
            img = ImageIO.read(new File(picture.getName()));

            Pixel[][] piecePixels = new Pixel[Config.delkaDilku][Config.delkaDilku];
            piecePixels[0][0] = new Pixel();
            for(int i = offsetX; i< offsetX + Config.delkaDilku; i++){
                for(int j = offsetY; j< offsetY + Config.delkaDilku; j++){
                    if(piecePixels[i-offsetX][j-offsetY] == null){
                        piecePixels[i-offsetX][j-offsetY] = new Pixel();
                    }
                    this.bf = img.getRGB(i, j);
                    int alfa = (this.bf >> 24) & 0xff;
                    int red = (this.bf >> 16) & 0xff;
                    int green = (this.bf >> 8) & 0xff;
                    int blue = (this.bf) & 0xff;

                    //prevedeni na HSB model
                     float[] hsb = new float[3];
                    Color.RGBtoHSB(red, green, blue, hsb);
                    piecePixels[i-offsetX][j-offsetY].setHsb(hsb);
                    
                    //piecePixels[i-offsetX][j-offsetY] = (red + green + blue )/3;      //uvazujeme jen jasovou slozku
                    piecePixels[i-offsetX][j-offsetY].setAlfa(alfa);
                    piecePixels[i-offsetX][j-offsetY].setRed(red);
                    piecePixels[i-offsetX][j-offsetY].setGreen(green);
                    piecePixels[i-offsetX][j-offsetY].setBlue(blue);
                }
            }
            piece.setPieceImage(piecePixels);
            return piece;
        }


        /**
         * Metoda, ktera zkopiruje hrany dilku na sousedni hrany.
         *
         * @param vstupniDilky
         * @return
         */
        public PuzzlePiece[][] copyEdges (PuzzlePiece[][] vstupniDilky){
            PuzzlePiece[][] vystupniDilky = new PuzzlePiece[Config.pocetDilkuX][Config.pocetDilkuY];

            vystupniDilky = vstupniDilky;

            for (int i=0; i < Config.pocetDilkuX; i++){ //Config.pocetDilkuX
                for (int j=0; j < Config.pocetDilkuY; j++){ //Config.pocetDilkuY
                    for (int k=0; k < Config.delkaDilku; k++){
                        //rozdily mezi dilky v radcich
                        if(i < Config.pocetDilkuY - 1){
                            vystupniDilky[i][j].getRight()[k].setRed(vystupniDilky[i+1][j].getLeft()[k].getRed());
                            vystupniDilky[i][j].getRight()[k].setGreen(vystupniDilky[i+1][j].getLeft()[k].getGreen());
                            vystupniDilky[i][j].getRight()[k].setBlue(vystupniDilky[i+1][j].getLeft()[k].getBlue());
                        }
                        //rozdily mezi dilky ve sloupcich
                        if(j < Config.pocetDilkuX - 1){
                            vystupniDilky[i][j].getDown()[k].setRed(vystupniDilky[i][j+1].getUp()[k].getRed());
                            vystupniDilky[i][j].getDown()[k].setGreen(vystupniDilky[i][j+1].getUp()[k].getGreen());
                            vystupniDilky[i][j].getDown()[k].setBlue(vystupniDilky[i][j+1].getUp()[k].getBlue());
                        }
                    }
                }
            }

            return vystupniDilky;
        }


        /**
         * Metoda rozreze vstupni obrazek na dany pocet dilku
         *
         * @return
         * @throws IOException
         */
        public PuzzlePiece[][] main() throws IOException {
            //pripraveni vzoroveho obrazku
            Picture picture = new Picture();
            picture.setName(Config.jmenoObrazku);
            picture.Init();

            //nacteni vsech dilku obrazku
            PuzzlePiece[][] puzzlePiecesAll = new PuzzlePiece[Config.pocetDilkuX][Config.pocetDilkuY];
            int id = 0;
            for (int i=0; i < Config.pocetDilkuX; i++){
                for (int j=0; j < Config.pocetDilkuY; j++){
                    puzzlePiecesAll[i][j]=this.readPiece(picture,i,j);
                    puzzlePiecesAll[i][j].setId(++id);
                    //pripravime kazdemu dilku znalost o svych ctyrech hranach
                    puzzlePiecesAll[i][j].setFourEdges(puzzlePiecesAll[i][j].getPieceImage());
                }
            }
            //return copyEdges(puzzlePiecesAll);
            return puzzlePiecesAll;
        }
    }
